home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************
- *
- * $Source: /unixb/home/unixlib/source/unixlib37/src/c/RCS/strtol,v $
- * $Date: 1996/04/19 21:26:42 $
- * $Revision: 1.1 $
- * $State: Rel $
- * $Author: simon $
- *
- * $Log: strtol,v $
- * Revision 1.1 1996/04/19 21:26:42 simon
- * Initial revision
- *
- ***************************************************************************/
-
- static const char rcs_id[] = "$Id: strtol,v 1.1 1996/04/19 21:26:42 simon Rel $";
-
- #include <ctype.h>
- #include <stdlib.h>
-
- int
- (atoi) (register const char *s)
-
- {
- return (atoi (s));
- }
-
- long
- (atol) (register const char *s)
-
- {
- return (atol (s));
- }
-
- #define digit(x) (((x) > '9') ? (((x) & 31) + 9) : ((x) - '0'))
-
- long
- strtol (register const char *s, char **end, register int b)
-
- {
- register long r = 0L;
- register int r_ = 0, d;
-
- if (!s)
- return (r);
-
- while (isspace (*s))
- s++;
-
- if (*s == '+' || *s == '-')
- {
- if (*s == '-')
- r_ = 1;
- s++;
- }
-
- if (!b)
- {
- if (*s == '0')
- {
- s++;
- b = 010;
- if (*s == 'x')
- {
- s++;
- b = 0x10;
- }
- }
- else
- b = 10;
- }
- else if (b == 16)
- if (*s == '0' && *++s == 'x')
- s++;
-
- while (d = digit (*s), d >= 0 && d < b)
- {
- r = r * (long) b + (long) d;
- s++;
- }
-
- if (end)
- *end = (char *) s;
-
- return (r_ ? -r : r);
- }
-
- unsigned long
- strtoul (register const char *s, char **end, register int b)
-
- {
- register unsigned long r = 0;
- register int d;
-
- if (!s)
- return (r);
-
- while (isspace (*s))
- s++;
-
- if (!b)
- {
- if (*s == '0')
- {
- s++;
- b = 010;
- if (*s == 'x')
- {
- s++;
- b = 0x10;
- }
- }
- else
- b = 10;
- }
- else if (b == 16)
- if (*s == '0' && *++s == 'x')
- s++;
-
- while (d = digit (*s), d >= 0 && d < b)
- {
- r = r * (unsigned long) b + (unsigned long) d;
- s++;
- }
-
- if (end)
- *end = (char *) s;
-
- return (r);
- }
-